<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
	function index() {
		if ($this->session->userdata('login')) {
			redirect('/welcome');
		}
		$this->load->view('login');
	}

	function check() {
		$u = $this->input->post('u', TRUE);
		$p = $this->input->post('p', TRUE);
		$this->load->model('cdn_user');
		if ($this->cdn_user->auth($u[0], $p[0])) {
			$this->init_seesion($u[0]);
			echo json_encode(array('r' => 'succ'));
		} else {
			echo json_encode(array('r' => 'fail'));
		}
	}
	
	function init_seesion($username) {
		$this->load->model('cdn_user');
		$user_info = $this->cdn_user->nfind($username);	
		$this->session->set_userdata('user_info', $user_info[0]);
		$module = array();
		$module['user'] = array(
					'cname' => '用户管理',
					'ename' => 'user',
					'sub_module' => array(
										0 => array(
											'cname' => '新增用户',
											'ename' => 'create'
										),
										1 => array(
											'cname' => '用户列表',
											'ename' => 'ulist'
										),
										2 => array(
											'cname' => '组列表',
											'ename' => 'glist'
										),
										3 => array(
											'cname' => '编辑用户',
											'ename' => 'edit'
										),
										4 => array(
											'cname' => '组管理',
											'ename' => 'group'
										)
									)
					);	
		$module['priv'] = array(
					'cname' => '权限管理',
					'ename' => 'priv',
					'sub_module' => array(
							0 => array(
								'cname' => '新增角色',
								'ename' => 'role'
							),
							1 => array(
								'cname' => '角色列表',
								'ename' => 'rlist'
							),
							2 => array(
								'cname' => '编辑角色',
								'ename' => 'redit'
							),
							3 => array(
								'cname' => '分配权限',
								'ename' => 'grant'
							)
					)
		);
		$module['domain'] = array(
					'cname' => '域名管理',
					'ename' => 'domain',
					'sub_module' => array(
										1 => array(
											'cname' => '域名增删',
											'ename' => 'manage'
										),
										0 => array(
											'cname' => '业务类型',
											'ename' => 'service'
										),
										2 => array(
											'cname' => '计费单元',
											'ename' => 'unit'
										),
										3 => array(
											'cname' => '域名绑定',
											'ename' => 'bind'
										)
									)
					);	
		$module['traffic'] = array(
					'cname' => '带宽信息',
					'ename' => 'traffic',
					'sub_module' => array(
										0 => array(
											'cname' => '计费流量',
											'ename' => 'flow'
										),
										1 => array(
											'cname' => '计费带宽',
											'ename' => 'bandwidth'
										),
										2 => array(
											'cname' => '域名带宽',
											'ename' => 'domain'
										)
									)
		);
		$module['content'] = array(
					'cname' => '内容管理',
					'ename' => 'content',
					'sub_module' => array(
										0 => array(
											'cname' => '清除缓存',
											'ename' => 'cache'
										),
										1 => array(
											'cname' => '缓存日志',
											'ename' => 'logs'
										)
									)
		);
		$module['log'] = array(
					'cname' => '日志分析',
					'ename' => 'log',
					'sub_module' => array(
										0 => array(
											'ename' => 'pv',
											'cname' => '访问量分析'
										),
										1 => array(
											'ename' => 'url',
											'cname' => '热门内容分析'
										),
										2 => array(
											'cname' => '访问来源分析',
											'ename' => 'referer'
										),
										3 => array(
											'cname' => '区域请求分析',
											'ename' => 'locale'
										),
										4 => array(
											'cname' => '来源地址分析',
											'ename' => 'rip'
										),
										5 => array(
											'cname' => 'HTTP响应吗分析',
											'ename' => 'code'
										)
									)
		);
		$this->session->set_userdata('module', $module);
		$this->session->set_userdata('login', TRUE);
	}
}
